package com.atguigu.orderservice.service.impl;

import com.atguigu.commonutils.Vo.EduCourseVo;
import com.atguigu.commonutils.Vo.UcenterMemberVo;
import com.atguigu.commonutils.utils.JwtUtils;
import com.atguigu.orderservice.client.EduClient;
import com.atguigu.orderservice.client.UcenterClient;
import com.atguigu.orderservice.entity.TOrder;
import com.atguigu.orderservice.mapper.TOrderMapper;
import com.atguigu.orderservice.service.TOrderService;
import com.atguigu.orderservice.utils.OrderUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author kaixuan
 * @since 2020-06-13
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    /**
     * 根据课程id和用户id创建订单 返回订单号
     * @param courseId 课程id
     * @param memberId 用户id
     * @return
     */
    @Override
    public String createOrder(String courseId, String memberId) {
        EduCourseVo courseVo = eduClient.getCourseInfo(courseId);
        UcenterMemberVo memberVo = ucenterClient.getMemberInfo(memberId);
        TOrder order = new TOrder();
        order.setOrderNo(OrderUtils.getOrderNo());
        order.setCourseId(courseVo.getId());
        order.setCourseTitle(courseVo.getTitle());
        order.setCourseCover(courseVo.getCover());
        order.setTeacherName(courseVo.getName());
        order.setMemberId(memberVo.getId());
        order.setNickname(memberVo.getNickname());
        order.setMobile(memberVo.getMobile());
        order.setTotalFee(courseVo.getPrice());
        order.setStatus(0);
        order.setPayType(1);
        baseMapper.insert(order);
        return order.getOrderNo();
    }
}
